package com.orhon.smartcampus.modules.baseinfo.fetcher;


import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.baseinfo.dao.TimetableDao;
import com.orhon.smartcampus.modules.baseinfo.entity.Timetable;
import com.orhon.smartcampus.modules.baseinfo.entity.TimetableItem;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLTimetable;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLTimetableItem;
import com.orhon.smartcampus.modules.baseinfo.mm.TimetableItemMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.TimetableMapper;

import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class TimetablesFetcher implements DataFetcher {


	@Autowired
	TimetableDao timetableDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLTimetable> GQLTimetable = new ArrayList<>();

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Timetable  where deleted_at = null and school_id = " + schoolId;

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("name")!=null) {
				hql += "and name  "+ Util.decide(map.get("name"));
			}
			if(map.get("school_id")!=null) {
				hql += "and school_id" + Util.decide(map.get("school_id"));
			}
			if(map.get("is_current")!=null) {
				hql += "and is_current" +  Util.decide(map.get("is_current"));
			}

		}

		List<Timetable> list = timetableDao.find(hql,null,page.getPage(),page.getLimit());

		for (Timetable item : list) {
			GQLSchool gqlSchool= null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			List<GQLTimetableItem> gqlTimetableItem= null;
			if (env.getSelectionSet().contains("data/items")){
				List<TimetableItem> items = item.getItems();
				gqlTimetableItem = TimetableItemMapper.MAPPER.toTimetableItemList(items);
			}

			GQLTimetable gqlTimetable = TimetableMapper.MAPPER.toTimetable(item);
			gqlTimetable.setSchool(gqlSchool);
			gqlTimetable.setItems(gqlTimetableItem);
			GQLTimetable.add(gqlTimetable);
		}
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = timetableDao.getTotalCount(hql);
		}

		return Result.ok().put("data" , GQLTimetable).put("total" , total);
	}


}
